<?php
header("Content-type: text/html; charset=gbk");
//Needed model file
include_once $_SERVER['DOCUMENT_ROOT']."/model/Stage.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Location.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/User.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Undertake.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Action.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Input.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Output.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/PictureOutput.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/TextOutput.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/EnumOutput.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/NumericalOutput.php";
//Needed dao file
include_once $_SERVER['DOCUMENT_ROOT']."/dao/StageDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/LocationDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/UserDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/UndertakeDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/ActionDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/InputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/PictureOutputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/TextOutputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/EnumOutputDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/NumericalOutputDAO.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/27
 * Time: 16:21
 */
class StageInfo{
    const PICTURE_OUTPUT = 0, TEXT_OUTPUT = 1, NUMERICAL_OUTPUT = 2, ENUM_OUTPUT = 3;
    public static function getStageInfo($stageId,$userId,$mode){
        $response = new StageBO();

        //The DAO of stage, location and undertake
        $stageDAO = new StageDAO();
        $locationDAO = new LocationDAO();
        $undertakeDAO = new UndertakeDAO();
        $stage = $stageDAO->query($stageId);

        //检查stage是否存在
        if($stage == null)
            return null;

        //Set the stage related info
        $response->setStageId($stage->getId());
        $response->setName($stage->getName());
        $response->setDesc($stage->getDescription());
        $response->setReward($stage->getReward());
        $response->setDdl($stage->getDeadline());
        $response->setContractTime(null);
        $response->setWorkerNum($stage->getWorkerNum());

        // Get and set the worker list if any
        $undertakes = $undertakeDAO->queryByStage($stageId);
        $workers = self::getWorkerList($undertakes);
        $response->setWorkerNames($workers);

        //Set the status of stage
        $workerNum = $stage->getWorkerNum();
        if($stage->getStatus() == Stage::$STAGE_ONGOING){
            if (count($undertakes) < $workerNum)
                $response->setStageStatus(Stage::$STAGE_IDLE);
            else
                $response->setStageStatus(Stage::$STAGE_ONGOING);
        }
        else
            $response->setStageStatus($stage->getStatus());

        // Get the info about start location and the input
        $src = $locationDAO->queryByStageAndType($stage->getId(), Location::$TYPE_SRC);
		$dest = $locationDAO->queryByStageAndType($stage->getId(), Location::$TYPE_DEST);
		$srcLocInputOutput = null;
		if($src != null)
		    $srcLocInputOutput = self::getInputOutputByLocation($src,$userId,$mode);
        $destLocInputOutput = null;
        if($dest != null)
            $destLocInputOutput = self::getInputOutputByLocation($dest,$userId,$mode);
        $locations = array();
        //Set the location, input and output
        array_push($locations,$srcLocInputOutput);
        array_push($locations,$destLocInputOutput);
		$response->setLocations($locations);

        //var_dump($response);
		//echo "<br /><br />";
		return $response;
    }

    //获得stage的worker列表
    private static function getWorkerList($undertakes){
        $workers = array();
		$userDAO = new UserDAO();
		foreach($undertakes as $undertake) {
            $worker = $userDAO->query($undertake->getUserId());
            array_push($workers,$worker->getAccount());
        }
		return $workers;
    }

    //根据location获得input和output
    private static function getInputOutputByLocation($location,$userId,$mode){
        $locationBO = new LocationBO();
        // DAO of the action, input and four types of output
        $actionDAO = new ActionDAO();
		$inputDAO = new InputDAO();
		$pictureoutputDAO = new PictureOutputDAO();
		$textoutputDAO = new TextOutputDAO();
		$enumoutputDAO = new EnumOutputDAO();
		$numberoutputDAO = new NumericalOutputDAO();

		// Get the input and output list
		$action = $actionDAO->queryActionListByLocation($location->getId())[0];
		$originalInputs = $inputDAO->queryInputListByAction($action->getId());
        //根据三种情况返回不同的output信息
        /*
         * 0 – 只查看stage有什么输出，比如在任务浏览页面
            * 1 – 作为worker查看自己的output记录
         * 2 – 作为requester查看stage所有的output
         */
        if($mode == 0){
            $pictureoutputs = $pictureoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
            $textoutputs = $textoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
            $enumoutputs = $enumoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
            $numberoutputs = $numberoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_DESC);
        }
        else if($mode == 1){
            $pictureoutputs = $pictureoutputDAO->queryOutputListByActionAndUser($action->getId(),$userId,Output::$OUTPUT_VALUE);
            $textoutputs = $textoutputDAO->queryOutputListByActionAndUser($action->getId(),$userId,Output::$OUTPUT_VALUE);
            $enumoutputs = $enumoutputDAO->queryOutputListByActionAndUser($action->getId(),$userId,Output::$OUTPUT_VALUE);
            $numberoutputs = $numberoutputDAO->queryOutputListByActionAndUser($action->getId(),$userId,Output::$OUTPUT_VALUE);
        }
        else{
            $pictureoutputs = $pictureoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_VALUE);
            $textoutputs = $textoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_VALUE);
            $enumoutputs = $enumoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_VALUE);
            $numberoutputs = $numberoutputDAO->queryOutputListByActionAndIndicator($action->getId(),Output::$OUTPUT_VALUE);
        }

        //根据数据库查询获得的input和output构建inputBO和outputBO
        $inputs = array();
        foreach($originalInputs as $originalInput){
            $input = new InputBO();
            $input->setId($originalInput->getId());
            $input->setActionId($originalInput->getActionId());
            $input->setType($originalInput->getType());
            $input->setDesc($originalInput->getDesc());
            $input->setValue($originalInput->getValue());
            array_push($inputs,$input);
        }
        //四种类型output
        $outputs = array();
        foreach ($pictureoutputs as $pictureoutput){
            $output = new OutputBO();
            $output->setId($pictureoutput->getId());
            $output->setActionId($pictureoutput->getActionId());
            $output->setIsActive($pictureoutput->getisActive());
            $output->setType(self::PICTURE_OUTPUT);
            $output->setDesc($pictureoutput->getDesc());
            $output->setValue($pictureoutput->getValue());
            array_push($outputs,$output);
        }
        foreach ($textoutputs as $textoutput){
            $output = new OutputBO();
            $output->setId($textoutput->getId());
            $output->setActionId($textoutput->getActionId());
            $output->setType(self::TEXT_OUTPUT);
            $output->setDesc($textoutput->getDesc());
            $output->setValue($textoutput->getValue());
            array_push($outputs,$output);
        }
        foreach ($enumoutputs as $enumoutput){
            $output = new OutputBO();
            $output->setId($enumoutput->getId());
            $output->setActionId($enumoutput->getActionId());
            $output->setType(self::ENUM_OUTPUT);
            $output->setDesc($enumoutput->getDesc());
            $output->setValue($enumoutput->getValue());
            $output->setEntries($enumoutput->getEntries());
            $output->setAggregaMethod($enumoutput->getAggregationMethod());
            array_push($outputs,$output);
        }
        foreach($numberoutputs as $numberoutput){
            $output = new OutputBO();
            $output->setId($numberoutput->getId());
            $output->setActionId($numberoutput->getActionId());
            $output->setType(self::NUMERICAL_OUTPUT);
            $output->setDesc($numberoutput->getDesc());
            $output->setValue($numberoutput->getValue());
            $output->setIntervalValue($numberoutput->getInterval());
            $output->setUpBound($numberoutput->getUpperBound());
            $output->setLowBound($numberoutput->getLowerBound());
            $output->setAggregaMethod($numberoutput->getAggregationMethod());
            array_push($outputs,$output);
        }

        //Set the information of locationBO
        $locationBO->setAddress($location->getAddress());
        $locationBO->setLongitude($location->getLongitude());
        $locationBO->setLatitude($location->getLatitude());
        $locationBO->setType($location->getType());
        $locationBO->setInputs($inputs);
        $locationBO->setOutputs($outputs);
        return $locationBO;
    }

}

class StageBO{
    public $stageId;
    public $name;
    public $desc;
    public $reward;
    public $ddl;
    public $contractTime;
    public $workerNum;
    public $workerNames;
    public $stageStatus;
    public $locations;

    /**
     * @return mixed
     */
    public function getStageId()
    {
        return $this->stageId;
    }

    /**
     * @param mixed $stageId
     */
    public function setStageId($stageId)
    {
        $this->stageId = $stageId;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = urlencode($name);
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param mixed $desc
     */
    public function setDesc($desc)
    {
        $this->desc = urlencode($desc);
    }

    /**
     * @return mixed
     */
    public function getReward()
    {
        return $this->reward;
    }

    /**
     * @param mixed $reward
     */
    public function setReward($reward)
    {
        $this->reward = $reward;
    }

    /**
     * @return mixed
     */
    public function getDdl()
    {
        return $this->ddl;
    }

    /**
     * @param mixed $ddl
     */
    public function setDdl($ddl)
    {
        $this->ddl = $ddl;
    }

    /**
     * @return mixed
     */
    public function getContractTime()
    {
        return $this->contractTime;
    }

    /**
     * @param mixed $contractTime
     */
    public function setContractTime($contractTime)
    {
        $this->contractTime = $contractTime;
    }

    /**
     * @return mixed
     */
    public function getWorkerNum()
    {
        return $this->workerNum;
    }

    /**
     * @param mixed $workerNum
     */
    public function setWorkerNum($workerNum)
    {
        $this->workerNum = $workerNum;
    }

    /**
     * @return mixed
     */
    public function getWorkerNames()
    {
        return $this->workerNames;
    }

    /**
     * @param mixed $workerNames
     */
    public function setWorkerNames($workerNames)
    {
        $this->workerNames = urlencode($workerNames);
    }

    /**
     * @return mixed
     */
    public function getStageStatus()
    {
        return $this->stageStatus;
    }

    /**
     * @param mixed $stageStatus
     */
    public function setStageStatus($stageStatus)
    {
        $this->stageStatus = $stageStatus;
    }

    /**
     * @return mixed
     */
    public function getLocations()
    {
        return $this->locations;
    }

    /**
     * @param mixed $locations
     */
    public function setLocations($locations)
    {
        $this->locations = $locations;
    }

}

class LocationBO{
    public $address;
    public $longitude;
    public $latitude;
    public $type;
    public $inputs;
    public $outputs;

    /**
     * @return mixed
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param mixed $address
     */
    public function setAddress($address)
    {
        $this->address = urlencode($address);
    }

    /**
     * @return mixed
     */
    public function getLongitude()
    {
        return $this->longitude;
    }

    /**
     * @param mixed $longitude
     */
    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;
    }

    /**
     * @return mixed
     */
    public function getLatitude()
    {
        return $this->latitude;
    }

    /**
     * @param mixed $latitude
     */
    public function setLatitude($latitude)
    {
        $this->latitude = $latitude;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getInputs()
    {
        return $this->inputs;
    }

    /**
     * @param mixed $inputs
     */
    public function setInputs($inputs)
    {
        $this->inputs = $inputs;
    }

    /**
     * @return mixed
     */
    public function getOutputs()
    {
        return $this->outputs;
    }

    /**
     * @param mixed $outputs
     */
    public function setOutputs($outputs)
    {
        $this->outputs = $outputs;
    }

}

class InputBO{
    public $id;
    public $actionId;
    public $type;
    public $desc;
    public $value;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getActionId()
    {
        return $this->actionId;
    }

    /**
     * @param mixed $actionId
     */
    public function setActionId($actionId)
    {
        $this->actionId = $actionId;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param mixed $desc
     */
    public function setDesc($desc)
    {
        $this->desc = urlencode($desc);
    }

    /**
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param mixed $value
     */
    public function setValue($value)
    {
        $this->value = urlencode($value);
    }

}

class OutputBO{
    public $id;
    public $actionId;
    public $type;
    public $desc;
    public $value;
    public $isActive;
    public $intervalValue;
    public $upBound;
    public $lowBound;
    public $entries;
    public $aggregaMethod;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getActionId()
    {
        return $this->actionId;
    }

    /**
     * @param mixed $actionId
     */
    public function setActionId($actionId)
    {
        $this->actionId = $actionId;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param mixed $desc
     */
    public function setDesc($desc)
    {
        $this->desc = urlencode($desc);
    }

    /**
     * @return mixed
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param mixed $value
     */
    public function setValue($value)
    {
        $this->value = urlencode($value);
    }

    /**
     * @return mixed
     */
    public function getisActive()
    {
        return $this->isActive;
    }

    /**
     * @param mixed $isActive
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    }

    /**
     * @return mixed
     */
    public function getIntervalValue()
    {
        return $this->intervalValue;
    }

    /**
     * @param mixed $intervalValue
     */
    public function setIntervalValue($intervalValue)
    {
        $this->intervalValue = $intervalValue;
    }

    /**
     * @return mixed
     */
    public function getUpBound()
    {
        return $this->upBound;
    }

    /**
     * @param mixed $upBound
     */
    public function setUpBound($upBound)
    {
        $this->upBound = $upBound;
    }

    /**
     * @return mixed
     */
    public function getLowBound()
    {
        return $this->lowBound;
    }

    /**
     * @param mixed $lowBound
     */
    public function setLowBound($lowBound)
    {
        $this->lowBound = $lowBound;
    }

    /**
     * @return mixed
     */
    public function getEntries()
    {
        return $this->entries;
    }

    /**
     * @param mixed $entries
     */
    public function setEntries($entries)
    {
        $this->entries = urlencode($entries);
    }

    /**
     * @return mixed
     */
    public function getAggregaMethod()
    {
        return $this->aggregaMethod;
    }

    /**
     * @param mixed $aggregaMethod
     */
    public function setAggregaMethod($aggregaMethod)
    {
        $this->aggregaMethod = $aggregaMethod;
    }

}
